package com.study.spmongodb.wwj.juc.utils;

import com.study.spmongodb.book.wwj.char23.CountDownLatch;

/**
 * @author MI
 * @version 1.0
 * @date 2020/12/12 8:14
 */
public class CountDownLatchExample2 {


    public static void main(String[] args) throws InterruptedException {
        final CountDownLatch latch = new CountDownLatch(1);
        new Thread(() -> {
            System.out.println("Do some initial working");
            try {
                Thread.sleep(1000);
                latch.await();
                System.out.println("Do other working……");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            System.out.println("async prepat  for some data.");
            try {
                Thread.sleep(2000);
                System.out.println("async prepare for done");


            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                try {
                    latch.countDown();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        Thread.currentThread().join();
    }
}
